package avicit.bdp.dgs.qa.service.algo.raw.data.pcm.utils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-11-01
 * @类说明：DataFileUtils
 * @修改记录：
 * @注意事项：
 * @主要功能：
 */
public class DataFileUtils {
    private BufferedInputStream input;
    public int count;

    public DataFileUtils(InputStream is) throws IOException {
        count = 0;
        this.input = new BufferedInputStream(is);
    }

    /***
     * 读取字节
     * @param b
     */
    public int readBytes(byte[] b) throws IOException {
        count += b.length;

        return input.read(b);
    }

    /***
     * 通过递归解决同步字连续判断问题
     * @param sync_short
     * @param checkIndex
     * @param data_sync_bytes
     * @throws IOException
     */
    public boolean checkIsSync(short[] sync_short, int checkIndex, byte[] data_sync_bytes) throws IOException {
        short data_sync_value = DataConvertUtils.byte2short(data_sync_bytes);
        int len = 0;
        if (data_sync_value == sync_short[checkIndex]) {
            if (checkIndex == (sync_short.length - 1)) {
                return true;
            } else if ((len = this.readBytes(data_sync_bytes)) != -1) {
                return checkIsSync(sync_short, checkIndex + 1, data_sync_bytes);
            }
        }

        return false;
    }

    public void skip(int skip_num) throws IOException {
        this.count += skip_num;
        this.input.skip(skip_num);
    }

    public void close() throws IOException {
        if (this.input != null) {
            this.input.close();
        }
    }
}
